package com.vecharm.app.ui.cloudshelf

import android.content.Intent
import android.graphics.Color
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.dongby.sdk.utils.DisplayUtils
import com.google.gson.Gson
import com.ling.sdk.app.App
import com.ling.sdk.utils.Toasty
import com.ling.sdk.utils.dip
import com.vecharm.app.R
import com.vecharm.app.bean.Goods
import com.vecharm.app.bean.GoodsBean
import com.vecharm.app.ui.BaseShowActivity
import com.vecharm.app.utils.TOKEN
import com.vecharm.app.utils.url
import kotlinx.android.synthetic.main.activity_goods_shelf.*


class GoodsShelfActivity : BaseShowActivity(), View.OnClickListener {


    override fun getLayoutId() = R.layout.activity_goods_shelf


    override fun onInitView() {
        window.addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)
        super.onInitView()
        dataToken = intent.getStringExtra(TOKEN)
        loadViewsForGoods(GoodsBean.findListByBrandId(dataToken))

        setBackgroundByConfig(background)
    }

    override fun onClick(v: View?) {
        val goods: Goods? = v?.tag as? Goods
        if (goods == null) {
            Toasty.showInfo("商品不存在，请返回")
            return
        }
        try {
            startActivity(Intent(this, GoodsDetailActivity::class.java).putExtra(TOKEN, Gson().toJson(goods)))
        } catch (e: Exception) {
        }

    }


    private fun loadViewsForGoods(oldList: List<Goods>?) {
        oldList ?: return
        val list = ArrayList<Goods>()
        oldList.forEach { list.add(it.copy()) }

        var spanCount = 0
        kotlin.run check@{
            var w = 0
            list.forEach {
                spanCount++
                w += GoodsBean.getGoodsSize(it).x.dip()
                if (w >= DisplayUtils.getScreenWidth()) return@check
            }
        }
        if (spanCount == 0) spanCount = 3


        val manager = StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL);//定义瀑布流管理器，第一个参数是列数，第二个是方向。

        listView.layoutManager = manager


        listView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
            override fun getItemCount() = list.size

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
                return object : RecyclerView.ViewHolder(layoutInflater.inflate(R.layout.item_cloud_shelf, parent, false)) {}
            }

            override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                val icon = holder.itemView.findViewById<ImageView>(R.id.icon)
                val name = holder.itemView.findViewById<TextView>(R.id.name)
                val desc = holder.itemView.findViewById<TextView>(R.id.desc).also { it.visibility = View.VISIBLE }
                val goods = list.getOrNull(position) ?: return
                val size = GoodsBean.getGoodsSize(goods)
                icon.layoutParams.apply { this.width = size.x.dip(); this.height = size.y.dip() }

                icon.url(App.app, goods.thumbnail)
                name.text = goods.title
                desc.text = goods.form
                holder.itemView.tag = goods
                holder.itemView.setOnClickListener(this@GoodsShelfActivity)

            }

        }

    }


    private fun setBackgroundByConfig(view: ImageView) {
        GoodsBean.getConfig().goodsShelfBackground?.also {
            view.visibility = View.VISIBLE
            view.url(App.app, it)
            return
        }

        GoodsBean.getConfig().goodsShelfBackgroundColor?.also {
            view.visibility = View.VISIBLE
            view.setBackgroundColor(Color.parseColor("#$it"))
            return
        }

    }

}


